package com.hjy.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hjy.product.dao.AttrAttrgroupRelationDao;
import com.hjy.product.entity.AttrAttrgroupRelationEntity;
import com.hjy.product.entity.ProductAttrValueEntity;
import com.hjy.product.service.ProductAttrValueService;
import com.hjy.product.vo.AttrRespVo;
import com.hjy.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hjy.product.entity.AttrEntity;
import com.hjy.product.service.AttrService;
import com.hjy.common.utils.PageUtils;
import com.hjy.common.utils.R;



/**
 * 商品属性
 *
 * @author hjy
 * @email 2402668109@qq.com
 * @date 2022-07-20 12:06:52
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    // /product/attr/base/listforspu/{spuId}
    @RequestMapping("/base/listforspu/{spuId}")
    public R getListForSpuBySpuId(@PathVariable("spuId") String spuId){
        QueryWrapper<ProductAttrValueEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("spu_id",spuId);
        List<ProductAttrValueEntity> list = productAttrValueService.list(wrapper);
        return R.ok().put("data",list);
    }

    // /product/attr/base/listforspu/{spuId}
    @RequestMapping("/update/{spuId}")
    public R updateBySpuId(@PathVariable("spuId") String spuId, @RequestBody List<ProductAttrValueEntity> list){
        QueryWrapper<ProductAttrValueEntity> wrapper = new QueryWrapper<>();

        // 先删除原有的
        wrapper.eq("spu_id",spuId);
        productAttrValueService.remove(wrapper);
        // 再新增
        productAttrValueService.saveBatch(list);

        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/{type}/list/{catelogId}")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params ,@PathVariable("type") String type, @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrService.queryPage(params,catelogId,type);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrEntity attr = attrService.getById(attrId);
        AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveInfo(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
        AttrEntity attrEntity = new AttrEntity();
        BeanUtil.copyProperties(attr,attrEntity);
        attrService.updateById(attrEntity);
        // 基本属性需要连表修改
        if (attr.getAttrType() == 1) {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrGroupId(attr.getAttrGroupId());
            attrAttrgroupRelationEntity.setAttrId(attr.getAttrId());
            Long cou = attrAttrgroupRelationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
            if (cou > 0) {
                attrAttrgroupRelationDao.update(attrAttrgroupRelationEntity,new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attrEntity.getAttrId()));
            }else {
                attrAttrgroupRelationDao.insert(attrAttrgroupRelationEntity);
            }
        }

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
